package co.jp.roomy;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import co.jp.roomy.vo.Heya;

/**
 * 新規部屋作成
 */
public class HeyaAddActivity extends Activity {
	
	private Heya heya;
	private DatabaseHelper helper = new DatabaseHelper(this);
	public static SQLiteDatabase db;
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.heya_add);
		
		// 部屋情報取得
		Intent intent = getIntent();
		heya = (Heya)intent.getSerializableExtra(HeyaAddActivity.this.getString(R.string.heyaKey));
		
		// 値をセット
		EditText heyaName = (EditText) findViewById(R.id.heyaName);
		heyaName.setText(heya.getHeyaName());
		EditText heyaSizeX = (EditText) findViewById(R.id.heyaSizeX);
		heyaSizeX.setText(String.valueOf(heya.getHeyaSizeX()));
		EditText heyaSizeY = (EditText) findViewById(R.id.heyaSizeY);
		heyaSizeY.setText(String.valueOf(heya.getHeyaSizeY()));
		EditText heyaSizeZ = (EditText) findViewById(R.id.heyaSizeZ);
		heyaSizeZ.setText(String.valueOf(heya.getHeyaSizeZ()));
		EditText heyaMemo = (EditText) findViewById(R.id.heyaMemo);
		heyaMemo.setText(heya.getHeyaMemo());
		
		// 部屋の色情報をセット
		ImageView colorView = (ImageView) findViewById(R.id.colorView);
		int color = Color.HSVToColor((new ColorpalletActivity().RGBtoHSV(heya.getHeyaColor())));
		Bitmap bitmap = Bitmap.createBitmap(new int[] { color }, 1, 1, Config.ARGB_8888);
		colorView.setImageBitmap(bitmap);
	}
	/**
	 * 部屋新規作成押下時
	 * @param View view
	 */
	public void heyaAddButtonOnClick(View view) {
		// 部屋情報を入力値から取得
		String heyaNameText = ((EditText) findViewById(R.id.heyaName)).getText().toString();
		heya.setHeyaName(heyaNameText);
		String heyaSizeX = ((EditText) findViewById(R.id.heyaSizeX)).getText().toString();
		heya.setHeyaSizeX(Integer.valueOf(heyaSizeX));
		String heyaSizeY = ((EditText) findViewById(R.id.heyaSizeY)).getText().toString();
		heya.setHeyaSizeY(Integer.valueOf(heyaSizeY));
		String heyaSizeZ = ((EditText) findViewById(R.id.heyaSizeZ)).getText().toString();
		heya.setHeyaSizeZ(Integer.valueOf(heyaSizeZ));
		String heyaMemo = ((EditText) findViewById(R.id.heyaMemo)).getText().toString();
		heya.setHeyaMemo(heyaMemo);
		
		// DB登録
		db = helper.getWritableDatabase();
		db.beginTransaction();
		ContentValues values = new ContentValues();
		values.put("heyaName", heya.getHeyaName());
		values.put("heyaSizeX", heya.getHeyaSizeX());
		values.put("heyaSizeY", heya.getHeyaSizeY());
		values.put("heyaSizeZ", heya.getHeyaSizeZ());
		values.put("heyaColor", heya.getHeyaColor());
		values.put("heyaMemo", heya.getHeyaMemo());
		db.insert("T_HEYA", null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
		
		// 画面遷移
		Intent intent = new Intent(HeyaAddActivity.this, MadoriActivity.class);
		intent.putExtra(HeyaAddActivity.this.getString(R.string.heyaKey), heya);
		startActivity(intent);
	}
	/**
	 * カラーピッカーボタン押下時
	 * @param View view
	 */
	public void colorpalletButtonOnClick(View view) {
		
		String heyaNameText = ((EditText) findViewById(R.id.heyaName)).getText().toString();
		heya.setHeyaName(heyaNameText);
		String heyaSizeX = ((EditText) findViewById(R.id.heyaSizeX)).getText().toString();
		heya.setHeyaSizeX(Integer.valueOf(heyaSizeX));
		String heyaSizeY = ((EditText) findViewById(R.id.heyaSizeY)).getText().toString();
		heya.setHeyaSizeY(Integer.valueOf(heyaSizeY));
		String heyaSizeZ = ((EditText) findViewById(R.id.heyaSizeZ)).getText().toString();
		heya.setHeyaSizeZ(Integer.valueOf(heyaSizeZ));
		String heyaMemo = ((EditText) findViewById(R.id.heyaMemo)).getText().toString();
		heya.setHeyaMemo(heyaMemo);
		
		Intent intent = new Intent(HeyaAddActivity.this, ColorpalletActivity.class);
		intent.putExtra(HeyaAddActivity.this.getString(R.string.heyaKey), heya);
		startActivity(intent);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		helper.close();
	}
}